package java_heap;

        public class Counter {
    private int count = 0;
 
    public void increment() {
        count++; // 这里的count是线程安全的，因为它是局部变量
    }
 
    public void printCount() {
        System.out.println(count);
    }
}
 
 class LocalVariableThreadSafety {
    public static void main(String[] args) {
        Counter counter = new Counter();
 
        // 创建两个线程，分别调用counter对象的increment方法
        Thread t1 = new Thread(counter::increment);
        Thread t2 = new Thread(counter::increment);
 
        t1.start();
        t2.start();
 
        // 等待线程执行完毕
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        // 打印count的值
        counter.printCount();
    }
}